package edu.zjut.sjq.controller;

import edu.zjut.sjq.bean.Article;
import edu.zjut.sjq.bean.Comment;
import edu.zjut.sjq.bean.User;
import edu.zjut.sjq.bean.Year;
import edu.zjut.sjq.service.ArticleService;
import edu.zjut.sjq.service.CommentService;
import edu.zjut.sjq.service.YearService;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.support.HttpRequestHandlerServlet;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * Created by sue on 2017/3/26.
 */
@Controller
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private CommentService commentService;

    /**
     * 获取当届可评分并且当前教师未评分的作品
     * @return
     */
    @RequestMapping("/getUncheckedArticle")
    public ModelAndView getUncheckedArticle(){
        ModelAndView modelAndView=new ModelAndView();
        List<Article> articleList = articleService.getUncheckedArticle();
        modelAndView.setViewName("article.jsp");
        modelAndView.addObject("title","评分作品");
        modelAndView.addObject("articleList",articleList);
        return modelAndView;
    }

    // 点攒数量和成绩序
    @RequestMapping("/getArticleRank")
    public ModelAndView getArticleRank(){
        ModelAndView modelAndView=new ModelAndView();
        List<Article> greatList = articleService.getArticleListOrderByGreat();
        List<Article> likeList = articleService.getArticleListOrderByLike();
        modelAndView.setViewName("rankpage.jsp");
        modelAndView.addObject("greatList",greatList);
        modelAndView.addObject("likeList",likeList);
        return modelAndView;
    }





    // 根据阿泰article id从数据库获取路径等描述
    @RequestMapping("/getById")
    @ResponseBody
    public Article getArticleById(@RequestParam Integer articleId){
        return articleService.getArticleById(articleId);
    }


    //获取作品详情页面
    // 加载主页
    @RequestMapping(value = "/getDetail/{articleId}",method = RequestMethod.GET)
    public ModelAndView getDetailById(@PathVariable("articleId") Integer articleId) throws Exception {
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("videodetailpage.jsp");
        //相关作品连接
        List<Comment> commentList = commentService.getComment(articleId, null);
        List<Article> articleList = articleService.getLatestArticle(commentList.size()+2,articleId);
        Article theArticle = articleService.getArticleById(articleId);
        modelAndView.addObject("articleList",articleList);
        modelAndView.addObject("commentList",commentList);
        modelAndView.addObject("theArticle",theArticle);

        return modelAndView;
    }





}
